package job_center

import (
	"context"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/utils"
	"gorm.io/gorm"
)

type jobCenterRepository struct {
	tenant_db.TenantDB
}

func NewJobCenterRepository() JobRepositoryIface {
	return &jobCenterRepository{}
}

func (r *jobCenterRepository) SaveJob(ctx context.Context, po *GfyxJobPo) (*GfyxJobPo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	return po, err
}

func (r *jobCenterRepository) QueryJobList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo, order string) (int64, []*GfyxJobPo, error) {
	var list []*GfyxJobPo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxJobPo{}.TableName())

	if len(condition) != 0 {
		if beginTime, ok := condition["begin_time"]; ok {
			db = db.Where("created_on > ?", beginTime)
			delete(condition, "begin_time")
		}
		if statusList, ok := condition["status_list"]; ok {
			db = db.Where("status in (?)", statusList)
			delete(condition, "status_list")
		}
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	if order == "" {
		order = "id desc"
	}

	err = db.Order("id desc").Find(&list).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, list, nil
		}
		return 0, nil, err
	}
	return count, list, nil
}

func (r *jobCenterRepository) UpdateJob(ctx context.Context, condition, updates map[string]interface{}) error {
	return r.GetTenantDB(ctx).Table(GfyxJobPo{}.TableName()).Where(condition).Updates(updates).Error
}

func (r *jobCenterRepository) QueryJobById(ctx context.Context, jobId int64) (*GfyxJobPo, error) {
	jobPo := GfyxJobPo{}
	err := r.GetTenantDB(ctx).Table(GfyxJobPo{}.TableName()).Where("id =? and is_deleted = ?", jobId, constants.IsDeletedFalse).First(&jobPo).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, err
	}
	return &jobPo, nil
}

func (r *jobCenterRepository) SaveJobBatchRuntime(ctx context.Context, po *GfyxJobBatchRuntimePo) (*GfyxJobBatchRuntimePo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	return po, err
}

func (r *jobCenterRepository) QueryJobBatchRuntimeList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo) (int64, []*GfyxJobBatchRuntimePo, error) {
	var list []*GfyxJobBatchRuntimePo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxJobBatchRuntimePo{}.TableName())

	if len(condition) != 0 {
		db = db.Where(condition)
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	err = db.Order("id").Find(&list).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, list, nil
		}
		return 0, nil, err
	}
	return count, list, nil
}

func (r *jobCenterRepository) DeleteJobBatchRuntimeByJobIds(ctx context.Context, jobIds []int64) error {
	return r.GetTenantDB(ctx).Table(GfyxJobBatchRuntimePo{}.TableName()).Where("job_id in (?)", jobIds).Update("is_deleted", constants.IsDeletedTrue).Error
}

func (r *jobCenterRepository) SaveJobFile(ctx context.Context, po *GfyxJobFilePo) (*GfyxJobFilePo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	return po, err
}

func (r *jobCenterRepository) QueryJobFileList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo) (int64, []*GfyxJobFilePo, error) {
	var list []*GfyxJobFilePo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxJobFilePo{}.TableName())

	if len(condition) != 0 {
		if jobIds, ok := condition["job_ids"]; ok {
			db = db.Where("job_id in (?)", jobIds)
			delete(condition, "job_ids")
		}
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	err = db.Order("id desc").Find(&list).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, list, nil
		}
		return 0, nil, err
	}
	return count, list, nil
}

func (r *jobCenterRepository) DeleteJobFileByJobIds(ctx context.Context, jobIds []int64) error {
	return r.GetTenantDB(ctx).Table(GfyxJobFilePo{}.TableName()).Where("job_id in (?)", jobIds).Update("is_deleted", constants.IsDeletedTrue).Error
}
